﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('Ap_CloseBills.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Ap_CloseBills.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="cCusVen">客户或供应商编码</a-select-option>
                <a-select-option key="cKm">科目编码</a-select-option>
                <a-select-option key="cXmClass">项目大类编码</a-select-option>
                <a-select-option key="cXm">项目编码</a-select-option>
                <a-select-option key="cDepCode">部门编码</a-select-option>
                <a-select-option key="cPersonCode">业务员编码</a-select-option>
                <a-select-option key="cOrderID">订单号</a-select-option>
                <a-select-option key="cItemName">项目名称</a-select-option>
                <a-select-option key="cConType">合同类型</a-select-option>
                <a-select-option key="cConID">合同号</a-select-option>
                <a-select-option key="cDLCode">发货单</a-select-option>
                <a-select-option key="ccItemCode">信用证项目</a-select-option>
                <a-select-option key="cDefine22">自定义项22</a-select-option>
                <a-select-option key="cDefine23">自定义项23</a-select-option>
                <a-select-option key="cDefine24">自定义项24</a-select-option>
                <a-select-option key="cDefine25">自定义项25</a-select-option>
                <a-select-option key="cDefine28">自定义项28</a-select-option>
                <a-select-option key="cDefine29">自定义项29</a-select-option>
                <a-select-option key="cDefine30">自定义项30</a-select-option>
                <a-select-option key="cDefine31">自定义项31</a-select-option>
                <a-select-option key="cDefine32">自定义项32</a-select-option>
                <a-select-option key="cDefine33">自定义项33</a-select-option>
                <a-select-option key="cStageCode">合同阶段</a-select-option>
                <a-select-option key="cCoVouchID">付款申请单号</a-select-option>
                <a-select-option key="cExecID">合同执行单号</a-select-option>
                <a-select-option key="cMemo">表体备注</a-select-option>
                <a-select-option key="cEBOrderCode">电商订单号</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{y:530}"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Ap_CloseBills.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('Ap_CloseBills.Edit')&&hasPerm('Ap_CloseBills.Delete')" type="vertical" />
          <a v-if="hasPerm('Ap_CloseBills.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import moment from 'moment'
const columns = [
  { title: '主表标识', dataIndex: 'iID', align: 'center', width: '10%' },
  { title: '子表标识', dataIndex: 'ID', align: 'center', width: '10%' },
  { title: '款项类型号', dataIndex: 'iType', align: 'center', width: '10%' },
  { title: '预收预付标志', dataIndex: 'bPrePay', align: 'center', width: '10%' },
  { title: '客户或供应商编码', dataIndex: 'cCusVen', align: 'center', width: '10%' },
  { title: '原币金额', dataIndex: 'iAmt_f', align: 'center', width: '10%' },
  { title: '本币金额', dataIndex: 'iAmt', align: 'center', width: '10%' },
  { title: '原币余额', dataIndex: 'iRAmt_f', align: 'center', width: '10%' },
  { title: '本币余额', dataIndex: 'iRAmt', align: 'center', width: '10%' },
  { title: '科目编码', dataIndex: 'cKm', align: 'center', width: '10%' },
  { title: '项目大类编码', dataIndex: 'cXmClass', align: 'center', width: '10%' },
  { title: '项目编码', dataIndex: 'cXm', align: 'center', width: '10%' },
  { title: '部门编码', dataIndex: 'cDepCode', align: 'center', width: '10%' },
  { title: '业务员编码', dataIndex: 'cPersonCode', align: 'center', width: '10%' },
  { title: '订单号', dataIndex: 'cOrderID', align: 'center', width: '10%' },
  { title: '项目名称', dataIndex: 'cItemName', align: 'center', width: '10%' },
  { title: '合同类型', dataIndex: 'cConType', align: 'center', width: '10%' },
  { title: '合同号', dataIndex: 'cConID', align: 'center', width: '10%' },
  { title: '数量', dataIndex: 'iAmt_s', align: 'center', width: '10%' },
  { title: '数量余额', dataIndex: 'iRAmt_s', align: 'center', width: '10%' },
  { title: '来源', dataIndex: 'iOrderType', align: 'center', width: '10%' },
  { title: '发货单', dataIndex: 'cDLCode', align: 'center', width: '10%' },
  { title: '信用证项目', dataIndex: 'ccItemCode', align: 'center', width: '10%' },
  { title: '登记标志', dataIndex: 'RegisterFlag', align: 'center', width: '10%' },
  { title: '自定义项22', dataIndex: 'cDefine22', align: 'center', width: '10%' },
  { title: '自定义项23', dataIndex: 'cDefine23', align: 'center', width: '10%' },
  { title: '自定义项24', dataIndex: 'cDefine24', align: 'center', width: '10%' },
  { title: '自定义项25', dataIndex: 'cDefine25', align: 'center', width: '10%' },
  { title: '自定义项26', dataIndex: 'cDefine26', align: 'center', width: '10%' },
  { title: '自定义项27', dataIndex: 'cDefine27', align: 'center', width: '10%' },
  { title: '自定义项28', dataIndex: 'cDefine28', align: 'center', width: '10%' },
  { title: '自定义项29', dataIndex: 'cDefine29', align: 'center', width: '10%' },
  { title: '自定义项30', dataIndex: 'cDefine30', align: 'center', width: '10%' },
  { title: '自定义项31', dataIndex: 'cDefine31', align: 'center', width: '10%' },
  { title: '自定义项32', dataIndex: 'cDefine32', align: 'center', width: '10%' },
  { title: '自定义项33', dataIndex: 'cDefine33', align: 'center', width: '10%' },
  { title: '自定义项34', dataIndex: 'cDefine34', align: 'center', width: '10%' },
  { title: '自定义项35', dataIndex: 'cDefine35', align: 'center', width: '10%' },
  { title: '自定义项36', dataIndex: 'cDefine36', align: 'center', width: '10%' },
  { title: '自定义项37', dataIndex: 'cDefine37', align: 'center', width: '10%' },
  { title: '合同阶段', dataIndex: 'cStageCode', align: 'center', width: '10%' },
  { title: '付款申请单号', dataIndex: 'cCoVouchID', align: 'center', width: '10%' },
  { title: '合同执行单号', dataIndex: 'cExecID', align: 'center', width: '10%' },
  { title: '表体备注', dataIndex: 'cMemo', align: 'center', width: '10%' },
  { title: '对应来源单据ID', dataIndex: 'iSrcClosesID', align: 'center', width: '10%' },
  { title: '费用结算金额', dataIndex: 'ifaresettled_f', align: 'center', width: '10%' },
  { title: '电商订单号', dataIndex: 'cEBOrderCode', align: 'center', width: '10%' },
  { title: '创建时间', dataIndex: 'CreateTime', width: '10%', align: 'center', scopedSlots: { customRender: 'CreateTime' } },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/OrderManagement/Ap_CloseBills/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建收付款单子表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑收付款单子表信息')
    },
    handleDelete (ids) {
      var thisObj = this
      // this.$confirm({
      //   title: '确认删除吗?',
      //   onOk () {
      //     return new Promise((resolve, reject) => {
      //       thisObj.$http.post('/OrderManagement/Ap_CloseBills/DeleteData', ids).then(resJson => {
      //         resolve()

      //         if (resJson.Success) {
      //           thisObj.$message.success('操作成功!')

      //           thisObj.getDataList()
      //         } else {
      //           thisObj.$message.error(resJson.Msg)
      //         }
      //       })
      //     })
      //   }
      // })
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        thisObj.$http
          .post('/OrderManagement/Ap_CloseBills/DeleteData', ids)
          .then(resJson => {
            // resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
      })
    },
    timeFormat (val) { // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
